from Tkinter import *
import Tkinter
import tkMessageBox

import jarvars
from save import saveConfiguration

class addLight(Frame):

    def __init__(self, master=None):
        
        rooms = []
        for ii in jarvars.Lights:
            rooms.append(ii[0])
            
            
        self.parent = master
        self.win = Toplevel(master)
        self.win.geometry("%dx%d%+d%+d" % (400, 100, 150, 0))
        Frame.__init__(self,self.win)
        self.room = StringVar()

        self.grid(padx=2, pady=2,sticky=N+S+E+W)


        self.roomLabel = Label(self, text="Room")
        self.lightToAdd = Entry(self, width=15)
        self.lightToAddLabel = Label(self, text="Light to add")
        self.addButton = Button(self, text="Add", command=self.addLight)
        if len(rooms) > 0:
            self.rooms = OptionMenu(self, self.room, *rooms)
            self.room.set(rooms[0])
        else:
            self.rooms = Label(self, text="No Current Rooms")

        self.roomLabel.grid(row=0, column=0, sticky=E)
        self.rooms.grid(row=0,column=1, sticky=W)
        self.lightToAddLabel.grid(row=1, column=0, sticky = E)
        self.lightToAdd.grid(row=1, column=1, sticky=W)
        self.addButton.grid(row=1,column=2, sticky=W)
        self.bind("<Destroy>", self._destroy)
        self.master.title("Add Light")
        
       
    def addLight(self):
        
        light = self.lightToAdd.get()
        if len(light) == 0:
            return
        room = self.room.get()
        print "Add Light", light, "to ", room

        for ii in jarvars.Lights:
            if ii[0] == room:
                ii[1].append(light)
        
        for ii in jarvars.State:
            if ii[0] == room:
                ii[1].append([light, 'No', 'No'])
        self.lightToAdd.delete(0,END)
        self.parent.updateStateList()
                
    def _destroy(self, Event=None):
        saveConfiguration()
        self.win.destroy()
    
if __name__ == "__main__":
    print "Not a main module"
    root = Tk()
    eu = addLight(root)
    while(1):
        eu.update()
    
    


